﻿Imports System
Imports System.Globalization
Imports System.Reflection
Imports System.Runtime.InteropServices

Namespace System.Workflow.Activities
    Friend NotInheritable Class ExternalDataExchangeBinder
        Inherits Binder
        ' Methods
        Friend Sub New()
        End Sub

        Public Overrides Function BindToField(ByVal bindingAttr As BindingFlags, ByVal match As FieldInfo(), ByVal value As Object, ByVal culture As CultureInfo) As FieldInfo
            Return Me.defltBinder.BindToField(bindingAttr, match, value, culture)
        End Function

        Public Overrides Function BindToMethod(ByVal bindingAttr As BindingFlags, ByVal match As MethodBase(), ByRef args As Object(), ByVal modifiers As ParameterModifier(), ByVal culture As CultureInfo, ByVal names As String(), <Out> ByRef state As Object) As MethodBase
            Dim array As Object() = New Object(args.Length  - 1) {}
            args.CopyTo(array, 0)
            state = Nothing
            Try 
                Return Me.defltBinder.BindToMethod(bindingAttr, match, (args), modifiers, culture, names, state)
            Catch exception1 As MissingMethodException
                If ((Not match Is Nothing) AndAlso (match.Length <> 0)) Then
                    Dim i As Integer
                    For i = 0 To match.Length - 1
                        Dim parameters As ParameterInfo() = match(i).GetParameters
                        If (parameters.Length = array.Length) Then
                            Dim j As Integer
                            For j = 0 To parameters.Length - 1
                                If (Not parameters(j).ParameterType.IsInstanceOfType(array(j)) AndAlso (Not parameters(j).ParameterType.IsArray OrElse (Not array(j) Is Nothing))) Then
                                    Exit For
                                End If
                                If ((j + 1) = parameters.Length) Then
                                    Return match(i)
                                End If
                            Next j
                        End If
                    Next i
                End If
            End Try
            Return Nothing
        End Function

        Public Overrides Function ChangeType(ByVal value As Object, ByVal type As Type, ByVal culture As CultureInfo) As Object
            Return Me.defltBinder.ChangeType(value, type, culture)
        End Function

        Public Overrides Sub ReorderArgumentArray(ByRef args As Object(), ByVal state As Object)
            Me.defltBinder.ReorderArgumentArray((args), state)
        End Sub

        Public Overrides Function SelectMethod(ByVal bindingAttr As BindingFlags, ByVal match As MethodBase(), ByVal types As Type(), ByVal modifiers As ParameterModifier()) As MethodBase
            Return Me.defltBinder.SelectMethod(bindingAttr, match, types, modifiers)
        End Function

        Public Overrides Function SelectProperty(ByVal bindingAttr As BindingFlags, ByVal match As PropertyInfo(), ByVal returnType As Type, ByVal indexes As Type(), ByVal modifiers As ParameterModifier()) As PropertyInfo
            Return Me.defltBinder.SelectProperty(bindingAttr, match, returnType, indexes, modifiers)
        End Function


        ' Fields
        Private defltBinder As Binder = Type.DefaultBinder
    End Class
End Namespace

